﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using WP7.InfoServiceRef;

namespace WP7
{
    public partial class Page1 : PhoneApplicationPage
    {
        System.Windows.Threading.DispatcherTimer dt = null;
        GeoCoordinate gc;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            init();
        }

        public Page1()
        {
            InitializeComponent();
        }

        private void init()
        {
            MainPage.gcw.TryStart(false, TimeSpan.FromMilliseconds(1000));

            buttonStop.Visibility = Visibility.Collapsed;
            buttonStart.IsEnabled = false;
            dt = new System.Windows.Threading.DispatcherTimer();

            gc = MainPage.gcw.Position.Location;

            MainPage.sref.GetLinesByTypeCompleted += new EventHandler<GetLinesByTypeCompletedEventArgs>(change);
            MainPage.sref.GetIDAsync((float)gc.Latitude, (float)gc.Longitude);
        }

        private void sendPosition(object sender, EventArgs e)
        {
            gc = MainPage.gcw.Position.Location;
                if (gc.IsUnknown != true)
                {
                    if (radioButton1.IsChecked == true || radioButton2.IsChecked == true || radioButton3.IsChecked == true || radioButton4.IsChecked == true)
                        if (listBox1.SelectedIndex >= 0)
                        {
                            MainPage.sref.InsertNewPositionAsync((int)listBox1.SelectedItem, MainPage.type, MainPage.MyIds, (float)gc.Latitude, (float)gc.Longitude);
                        }
                }
        }

        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            listBox1.IsEnabled = false;
            radioButton1.IsEnabled = false;
            radioButton2.IsEnabled = false;
            radioButton3.IsEnabled = false;
            radioButton4.IsEnabled = false;
            buttonStart.Visibility = System.Windows.Visibility.Collapsed;
            buttonStop.Visibility = System.Windows.Visibility.Visible;
            dt.Interval = new TimeSpan(0, MainPage.second / 60, MainPage.second % 60);
            dt.Tick += new EventHandler(sendPosition);
            dt.Start();
        }

        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {
            dt.Stop();
            listBox1.IsEnabled = true;
            radioButton1.IsEnabled = true;
            radioButton2.IsEnabled = true;
            radioButton3.IsEnabled = true;
            radioButton4.IsEnabled = true;
            buttonStop.Visibility = System.Windows.Visibility.Collapsed;
            buttonStart.Visibility = System.Windows.Visibility.Visible;
        }

        private void QueryNotFound()
        {
            listBox1.IsEnabled = false;
            List<String> elemek = new List<String>(); ;
            
            elemek.Add("Sajnos ezek még nem szerepelnek");
            elemek.Add("az adatbázisban");

            listBox1.ItemsSource = elemek;
        }

        private void change(object sender, GetLinesByTypeCompletedEventArgs e)
        {
            listBox1.ItemsSource = new List<int>();
            listBox1.IsEnabled = true;
            if (e.Error != null) { NavigationService.Navigate(new Uri("/../MainPage.xaml?error=dberror", UriKind.Relative)); }
            else
            {
                if (e.Result.Count == 0) QueryNotFound();
                else
                {
                    listBox1.ItemsSource = e.Result.ToList();
                }
            }
        }

        private void selectMachine(object sender, RoutedEventArgs e)
        {
                 if (radioButton1.IsChecked == true) { MainPage.type = "M"; }
            else if (radioButton2.IsChecked == true) { MainPage.type = "V"; }
            else if (radioButton3.IsChecked == true) { MainPage.type = "B"; }
            else if (radioButton4.IsChecked == true) { MainPage.type = "T"; }
            MainPage.sref.GetLinesByTypeAsync(MainPage.type);
        }

        private void backToMenu(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/../MainPage.xaml", UriKind.Relative));
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            dt.Stop();
            base.OnNavigatingFrom(e);
        }

        private void selected(object sender, SelectionChangedEventArgs e)
        {
            buttonStart.IsEnabled = true;
        }
    }
}                 